package com.open.party.cloud.view

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import cn.android.x.model.data.ResultInfo
import com.open.party.cloud.MainActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.PartyOrgBean
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.comm.view.scrollViewGridView.FunItemAdapter
import com.open.party.cloud.view.home.jiCengDangJian.OrgForCityCommMainListActivity
import com.open.party.cloud.view.home.juBao.JuBao12380ChaXunSearchActivity
import com.open.party.cloud.view.home.juBao.JuBao12380MainActivity
import com.open.party.cloud.view.home.juBao.JuBao12380MainAreaActivity
import com.open.party.cloud.view.home.juBao.JuBao12380RuXuZhiActivity
import com.open.party.cloud.view.home.renCaiGongZuo.ZhuanJiaMainActivity
import com.open.party.cloud.view.login.LoginActivity
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.main_fragment_org_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


open class OrgMainFragment : OrgMainBaseFragment(), View.OnClickListener {

    private var currView: View? = null
    private var parentActivity: MainActivity? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        parentActivity = context as MainActivity
    }

    private fun initTitleView(view: View) {
        titleBarView = view.findViewById(R.id.titleBarView)
        titleBarView!!.setCenterTxt("功能")
        titleBarView!!.setCenterTxtColor(R.color.page_title)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (currView == null) {
            currView = inflater.inflate(R.layout.main_fragment_org_main, container, false)
        }
        return currView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initTitleView(view)
        initView()
        initItemView()
    }

    private fun initView() {
        item01RootView.setOnClickListener(this)
        rencaiItem.setOnClickListener { gotoRenCaiGongGao(0) }
        zhuanJiaItem.setOnClickListener { XUtils.intent().openActivity(activity, ZhuanJiaMainActivity::class.java).startInFragment(this) }

        juBaoXuZhiItem.setOnClickListener {
            XUtils.intent().openActivity(activity, JuBao12380RuXuZhiActivity::class.java)
                .startInFragment(this)
        }
        fanKuiItem.setOnClickListener {
            XUtils.intent().openActivity(activity, JuBao12380ChaXunSearchActivity::class.java)
                .startInFragment(this)
        }
    }

    private fun initItemView() {
//        val adapter = FunItemAdapter(activity, getIconTxt())
//        gridView.adapter = adapter
//        adapter.setOnSuperListener(this)

        val adapter2 = FunItemAdapter(activity, getIconTxt2())
        gridView2.adapter = adapter2
        adapter2.setOnSuperListener(this)

        val adapter3 = FunItemAdapter(activity, getIconTxt3())
        gridView3.adapter = adapter3
        adapter3.setOnSuperListener(this)

        val adapter4 = FunItemAdapter(activity, getIconTxt4())
        gridView4.adapter = adapter4
        adapter4.setOnSuperListener(this)

        cityDoubleNewItem.setOnClickListener(this)
        dangJianGuoQiItem.setOnClickListener(this)


//        val adapter5 = FunItemAdapter(activity, getIconTxt5())
//        gridView5.adapter = adapter5
//        adapter5.setOnSuperListener(this)

//        val adapter6 = FunItemAdapter(activity, getIconTxt6())
//        gridView6.adapter = adapter6
//        adapter6.setOnSuperListener(this)

//        val adapter7 = FunItemAdapter(activity, getIconTxt7())
//        gridView7.adapter = adapter7
//        adapter7.setOnSuperListener(this)

//        val adapter8 = FunItemAdapter(activity, getIconTxt8())
//        gridView8.adapter = adapter8
//        adapter8.setOnSuperListener(this)

        val adapter9 = FunItemAdapter(activity, getIconTxt9())
        gridView9.adapter = adapter9
        adapter9.setOnSuperListener(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<PartyOrgBean>>) {
    }

    override fun onClick(v: View?) {
        when (v) {
//            item03RootView -> {
//                XUtils.intent().openActivity(parentActivity, OrgForCountryMainListActivity::class.java).startInFragment(this)
//            }

            cityDoubleNewItem -> {
                gotoJiChuDangJian(1)
            }
            dangJianGuoQiItem -> {
                gotoJiChuDangJian2(0)
            }
        }

        // 需要登录项
        if (!LocalCache.getAutoLogin()) {
            // 登录检测
            XUtils.intent().openActivity(parentActivity, LoginActivity::class.java)
                .startInFragment(this)
            return
        }

//        // 游客不能用
//        if ("TOURIST" == LocalCache.getLoginRole()) {
//            XUtils.toast().show("你没有权限")
//            return
//        }
//
//        // 志愿者不能用
//        if ("VOLUNTEER" == LocalCache.getLoginRole()) {
//            XUtils.toast().show("志愿者不能用")
//            return
//        }

        when (v) {
            item01RootView -> {
//                // 志愿者管理员不能用
//                if ("VOLUNTEER" == LocalCache.getLoginRole()) {
//                    XUtils.toast().show("志愿者管理员不能用")
//                    return
//                }
//                XUtils.intent().openActivity(parentActivity, OrgForCityCommMainListActivity::class.java).startInFragment(this)

                XUtils.intent().openActivity(parentActivity, JuBao12380MainAreaActivity::class.java).startInFragment(this)
            }
//            item02RootView -> { // OrgPartyTreeMainActivity
//                XUtils.intent().openActivity(parentActivity, OrgPartyListMainActivity::class.java).startInFragment(this)
//            }

//            cityDoubleNewItem -> {
//                gotoJiChuDangJian(1)
//            }
//            dangJianGuoQiItem -> {
//                gotoJiChuDangJian2(0)
//            }
        }
    }
}